package com.dx.mes.common.transcoder.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;

import java.io.IOException;

/**
 *  
 *Module:       HttpClientUtil.java 
 *Description:  以get/post的方式发送数据到指定的http接口---利用httpclient.jar包---HTTP接口的调用 
 *Company:       
 *Author:       ptp 
 *Date:         Feb 22, 2012 
 */
public class HttpClientUtil {

	private static final Logger logger = Logger.getLogger(HttpClientUtil.class);

	private String url;
	/** 
	 * post方式  
	 * @param param1  
	 * @param param2 
	 * @return 
	*/
	public String postHttp(String pallet_id) {
		String responseMsg = "";

		//1.构造HttpClient的实例  
		HttpClient httpClient = new HttpClient();

		httpClient.getParams().setContentCharset("UTF-8");

		//2.构造PostMethod的实例  
		PostMethod postMethod = new PostMethod(url);

		//3.把参数值放入到PostMethod对象中  
		postMethod.addParameter("palletId", pallet_id);

		try {
			// 4.执行postMethod,调用http接口  
			httpClient.executeMethod(postMethod);//200  

			//5.读取内容  
			responseMsg = postMethod.getResponseBodyAsString().trim();
			logger.info(responseMsg);

			//6.处理返回的内容  

		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//7.释放连接  
			postMethod.releaseConnection();
		}
		return responseMsg;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}